package com.schoolvisitors.repository;

import com.schoolvisitors.entity.Room;
import jakarta.persistence.LockModeType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface RoomRepository extends JpaRepository<Room, Long> {

    boolean existsByNumberAndFloorId(Long number, Long floorId);

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("select r from Room r where r.id = :id")
    Optional<Room> findByIdForUpdate(@Param("id") Long id);

    Room findByFloorIdAndNumber(Long floorId, Long number);

    Page<Room> findAllByNumber(Long number, Pageable pageable);

    boolean existsByFloorId(Long floorId);

    Optional<Room> findByIdAndDeleteFalse(Long id);

    Page<Room> findAllByDeleteFalse(Pageable pageable);
    List<Room> findAllByDeleteFalse(Sort pageable);

    Page<Room> findAllByNumberAndDeleteFalse(Long number, Pageable pageable);

    boolean existsByFloorIdAndDeleteFalse(Long floorId);

    List<Room> findAllByDeleteFalse();
}

